package com.sjy.shopmall.controller;

import com.sjy.shopmall.pojo.User;
import com.sjy.shopmall.service.UserService;
import com.sjy.shopmall.vo.ResultVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    UserService userService;

    @GetMapping("register")
    ResultVO reg(String username, String password) {
        int r = userService.register(username, password);
        if (r == 1) {
            return new ResultVO(ResultVO.success, null, null);
        } else {
            return new ResultVO(ResultVO.fail, null, null);
        }
    }

    @GetMapping("login")
    ResultVO login(String username, String password) {
        String r = userService.login(username, password);
        if (r != null) {
            return new ResultVO(ResultVO.success, null, r);
        } else {
            return new ResultVO(ResultVO.fail, "登录失败", null);
        }
    }

    @GetMapping("info")
    ResultVO info(long token) {
        User user = userService.getUserById(token);
        if (user == null) {
            return new ResultVO(ResultVO.fail, "错误的token", null);
        } else {
            return new ResultVO(ResultVO.success, null, user);
        }
    }


}
